﻿/// <reference path="../../lib/underscore.js" />
// For an introduction to the Page Control template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232511
(function () {
    "use strict";

    WinJS.UI.Pages.define("/pages/assetspecs/assetspecs.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.
            var currentState = WinJS.Navigation.state;

            if (currentState) {
                $(".pagetitle").text(currentState.symbol + " Specifics - " + currentState.startDate);


                var assetCalc = currentState.calculator;
                setChart(assetCalc.asset.symbol, _.min(assetCalc.asset.prices, function (price) { return price.date.getValue(); }).date, assetCalc.asset.prices);
                assetCalc.getDailyMeanRate().done(function (value) { dailyrate.innerText = Math.round((value * 100) * Math.pow(10, 2)) / Math.pow(10, 2) + "%"; });
                assetCalc.getAnnualizedMeanRate().done(function (value) { annualrate.innerText = Math.round((value * 100) * Math.pow(10, 2)) / Math.pow(10, 2) + "%"; });
                assetCalc.getDailyStandardDeviation().done(function (value) { dailystdev.innerText = Math.round((value * 100) * Math.pow(10, 2)) / Math.pow(10, 2) + "%"; });
                assetCalc.getAnnualizedStandardDeviation().done(function (value) { annualstdev.innerText = Math.round((value * 100) * Math.pow(10, 2)) / Math.pow(10, 2) + "%"; });
                assetCalc.getSharpeRatio().done(function (value) { sharperatio.innerText = Math.round(value * Math.pow(10, 2)) / Math.pow(10, 2); });
                assetCalc.getBeta().done(function (value) { betarisk.innerText = Math.round(value * Math.pow(10, 2)) / Math.pow(10, 2); });
                assetCalc.getHistoricalCorrelation().done(function (value) { correlation.innerText = Math.round((value * 100) * Math.pow(10, 2)) / Math.pow(10, 2) + "%"; });
            }
        },

        unload: function () {
            // TODO: Respond to navigations away from this page.
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in viewState.
        }
    });

    function setChart(name, date, AssetPriceItems) {
        var stockData = new Array(AssetPriceItems.length);

        for (var i = 0; i < stockData.length; i++) {
            stockData[i] = [Date.UTC(AssetPriceItems[i].date.year, AssetPriceItems[i].date.month, AssetPriceItems[i].date.date), AssetPriceItems[i].adjustedClosePrice];
        }


        var Chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'spline',
                backgroundColor: null
            },

            title: {
                text: null
            },

            xAxis: {
                type: 'datetime',
                title: {
                    text: 'Date',
                    style: {
                        color: '#FFFFFF',
                        'font-weight': 400,
                        'font-family': "Segoe UI Light",
                        'font-size': "18px"
                    }
                }
            },

            yAxis: {
                type: 'float',
                title: {
                    text: 'Stock price',
                    style: {
                        color: '#FFFFFF',
                        'font-weight': 400,
                        'font-family': "Segoe UI Light",
                        'font-size': "18px"
                    }
                }
            },

            legend: {
                style: {
                    'font-family': "Segoe UI Light"
                }
            },

            tooltip: {
                style: {
                    'font-family': "Segoe UI Light"
                }
            },

            series: [{
                name: "Closing Price",
                color: "#0094ff",
                marker: {
                    radius: 4
                },
                lineWidth: 4,
                data: stockData
            }]
        });
    }
})();
